package com.sky.controller.user;


import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result<AddressBook> save(@RequestBody AddressBook addressBook){
        addressBookService.save(addressBook);
        return Result.success(addressBook);
    }

    /**
     * 更新地址
     * @param addressBook
     * @return
     */

    @PutMapping
    public Result<AddressBook> update(@RequestBody AddressBook addressBook){
        addressBookService.update(addressBook);
        return Result.success(addressBook);
    }

    /**
     * 获取默认地址
     * @return
     */
    @GetMapping("/default")
public Result<AddressBook> defaults(){
        AddressBook addressBook=addressBookService.defaults();
        return Result.success(addressBook);
    }

    /**
     * 获取地址列表
     * @return
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        List<AddressBook> list = addressBookService.list();
        return Result.success(list);
    }

    /**
     * 根据id获取地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable("id") Long id){
        AddressBook addressBook=addressBookService.getById(id);
        return Result.success(addressBook);


    }

    /**
     * 删除id删除地址
     * @param id
     * @return
     */

    @DeleteMapping
    public Result delete(@RequestParam("id") Long id){
        addressBookService.delete(id);
        return Result.success();
    }


    @PutMapping("/default")
    public Result setDefault(@RequestBody Map<String,Long> jsobj){
        Long id=jsobj.get("id");
        addressBookService.setDefault(id);
        return Result.success();
    }




}
